JPProf简单使用

您所在的位置:网站首页 关闭go pprof对外的访问 JPProf简单使用

JPProf简单使用

#JPProf简单使用| 来源: 网络整理| 查看: 265

Grafana jpprof是Grafana Phlare官方为支持java语言开发的一个包。

说明

JPProf将go pprof工具带入java世界。

该库实现了一个http处理程序,它可以以pprof可视化工具所期望格式来提供运行时分析数据

使用参考 添加依赖项

如果你使用的是maven,请将一下依赖项添加到你的pom.xml

 com.grafana  jpprof  0.1.0

如果你使用的是gradle,请将以下依赖项添加到你的build.gradle

dependencies { // ....    implementation 'com.grafana:jpprof:0.1.0' //... } 与Springboot集成

如果你使用的是Spring Boot,则可以将以下控制器添加到你的应用程序中:

package com.example.springboot; ​ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; ​ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.time.Duration; ​ import jpprof.CPUProfiler; ​ @RestController public class PprofController { ​  @GetMapping("/debug/pprof/profile")  @ResponseBody  public void profile(@RequestParam(required = false) String seconds, HttpServletResponse response) {    try {      Duration d = Duration.ofSeconds(Integer.parseInt(seconds));      CPUProfiler.start(d, response.getOutputStream());      response.flushBuffer();   } catch (Exception e) {      System.out.println("exception: " + e.getMessage());   } } } 与独立服务器集成

如果你使用的是Spring Boot以外的其他框架,你仍然可以通过在代码中添加以下行来使用该库:

package com.example; ​ import java.net.InetSocketAddress; import com.sun.net.httpserver.HttpServer; import jpprof.PprofHttpHandler; ​ public class Main { ​    public static void main(String[] args) throws Exception {        //.... your main code ...        var server = HttpServer.create(new InetSocketAddress(8080), 0);        server.createContext("/", new PprofHttpHandler());        server.start();   } ​ }

这将使用监听 port 的新服务器公开根路径上的 pprof 端点8080

示例

这里使用spring boot+maven。注意、注意、注意、 项目需要最低基于java11,使用java8报错

创建springboot项目,参考上面的配置加上控制器代码及pom.xml。

❯ tree                                                                                                                                                     ─╯ . ├── HELP.md ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml ├── pprof-java-demo.iml ├── src │   ├── main │   │   ├── java │   │   │   └── com │   │   │       └── example │   │   │           └── pprofjavademo │   │   │               ├── Controller.java │   │   │               ├── PprofController.java │   │   │               └── PprofJavaDemoApplication.java │   │   └── resources │   │       ├── application.properties │   │       ├── static │   │       └── templates │   └── test │       └── java │           └── com │               └── example │                   └── pprofjavademo │                       └── PprofJavaDemoApplicationTests.java └── target   ├── classes   │   ├── application.properties   │   └── com   │       └── example   │           └── pprofjavademo   │               ├── Controller.class   │               ├── PprofController.class   │               └── PprofJavaDemoApplication.class   └── generated-sources       └── annotations ​

启动项目,访问一下

#启动正常 curl localhost:8080/hello 你好 SpringBoot

获取cpu配置文件(本地需要安装Graphviz)

go tool pprof -http :6060 "http://localhost:8080/debug/pprof/profile?seconds=10" ​ Fetching profile over HTTP from http://localhost:8080/debug/pprof/profile?seconds=10

zCwUc8.png



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3